#!/bin/bash
# discription:
#   auto upload bt downloads to nutstore
# Usage:
#   with crontab
# Author: sid(cnqdcyq@163.com)
# Date: 2021/9/3

echo "###########################"
echo "$(date) atupload.sh: auto upload bt downloads to nutstore"
cd /var/local/btdownload
echo "Change working directory to: $(pwd)"
####################################################################
# Step1: get hash code for comparing, to check whether files have 
# been changed.

# get account for transmission-remote
BT_USER=$(cat "$HOME/.mypasswd/.transmission.username")
BT_PASSWD=$(cat "$HOME/.mypasswd/.transmission.passwd")

# get bt task info, now
transmission-remote --auth "$BT_USER":"$BT_PASSWD" -l > torrent.list
md5new=$(md5sum torrent.list | gawk '{print $1}')

# get old bt task info
if test -e torrent_old.list
then
    md5old=$(md5sum torrent_old.list| gawk '{print $1}')
else
    echo "Error:No torrent old list exist!"
    exit 1
fi

####################################################################
# Step2: check whether there is new file 
if [ "$md5old" = "$md5new" ]
then
    echo "Check: Same File."
    echo "Finish: Nothing to do, Exit."
    exit 2
else
    echo "Check: File changed"
    ########################################################################
    # Step3: Check whether there is dowloading task
    # Get info about tasks, save as torrent_temp.txt
    LineNo=$(gawk '{print NR}' torrent.list | tail -n 1)
    if [ "$LineNo" = 2 ]
    then
        echo "Check: No bt task."
        echo "Finish: Nothing to do, exit."
        exit 3
    fi

    cat torrent.list | tail -n +2 | head -n -1 > torrent_temp.txt
    NoUnFinished=$(cat torrent_temp.txt | grep -v -c 100%)
    if [ "$NoUnFinished" = 0 ]
    then
        ###################################################################
        # Step4: there is new file and downloading task finished, then find
        # the name of the new file/dir
        cat torrent_temp.txt | gawk -F' {2,}' '{print $10}' > Names_new.txt
        cat torrent_old.list | tail -n +2 | head -n -1 | gawk -F' {2,}' \
            '{print $10}' > Names_old.txt
        IFS_old=$IFS
        IFS=$'\n'
        Names_new=($(cat Names_new.txt))
        Names_old=($(cat Names_old.txt))
        IFS=$IFS_old
        for item in "${Names_new[@]}"
        do
            echo "BtTask: $item"
            if [[ "${Names_old[@]}" =~ "$item" ]]
            then
                echo "item has been uploaded."
            else
                echo "item hasn't been uploaded."
                if test -e nohup.out
                then
                    rm "nohup.out"
                fi
                echo "Check: Uploading..."
                nohup upload.sh "$item" &
            fi
        done
        mv torrent.list torrent_old.list
    else
        echo "Check: There is downloading task."
        echo "Finish: Nothing to do ,exit."
        exit 4
    fi
fi

rm Names_new.txt Names_old.txt torrent_temp.txt
echo "Check: Temp file removed."
echo "Finish: All work finished."
